/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package AccesoWS;

import DTOs.DTOAerolinea;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import aerolinea.Ciudad;
import aerolinea.Cliente;
import aerolinea.Reserva;
import aerolinea.Vuelo;

/**
 *
 * @author Julian
 */
public class WSAerolinea {

    static public boolean autenticaraerolinea(String login, String pass) throws Exception {
        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here
            aerolinea.Cliente c = new aerolinea.Cliente();
            c.setLogin(login);
            c.setClave(pass);
            // TODO process result here
            boolean result = port.autenticarCliente(c);
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }
    }

    static public boolean crearclienteaerolinea(String login, String pass, String name, String lastname, String phone) throws Exception {

        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here
            aerolinea.Cliente cliente = new aerolinea.Cliente();
            cliente.setLogin(login);
            cliente.setClave(pass);
            cliente.setNombre(name);
            cliente.setApellido(lastname);
            cliente.setTelefono(Long.parseLong(phone));

            // TODO process result here
            boolean result = port.crearCliente1(cliente);
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }
    }

    static public Cliente getclienteaerolinea(String username) throws Exception {

        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here
            // TODO process result here
            aerolinea.Cliente result = port.retornarClienteByUsername(username);
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }
    }

    static public List<Ciudad> getciudades() throws Exception {

        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO process result here
            java.util.List<aerolinea.Ciudad> result = port.consultarCiudades();
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }

    }

    static public List<Vuelo> getallvuelos() throws Exception {
        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO process result here
            java.util.List<aerolinea.Vuelo> result = port.consultarVuelos();
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }
    }

    static public Ciudad getciudadByID(int idciudad) throws Exception {

        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here
            long id = (long) idciudad;
            // TODO process result here
            aerolinea.Ciudad result = port.retornarCiudadById(id);
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }

    }

    static public List<Vuelo> getvuelos(int idorigen, int iddestino, int year, int month, int day) throws Exception {


        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here


            DatatypeFactory factory = DatatypeFactory.newInstance();
            XMLGregorianCalendar fecha = factory.newXMLGregorianCalendar();
            fecha.setDay(day);
            fecha.setMonth(month);
            fecha.setYear(year);
            fecha.setHour(0);
            fecha.setMinute(0);
            fecha.setSecond(0);

            aerolinea.Ciudad origen = getciudadByID(idorigen);
            aerolinea.Ciudad destino = getciudadByID(iddestino);
            // TODO process result here
            java.util.List<aerolinea.Vuelo> result = port.consultarVuelosByDate1(fecha, origen, destino);
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }
    }

    static public List<Vuelo> getvuelo(int year, int month, int day) throws Exception {



        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here
            DatatypeFactory factory = DatatypeFactory.newInstance();
            XMLGregorianCalendar fecha = factory.newXMLGregorianCalendar();
            fecha.setDay(day);
            fecha.setMonth(month);
            fecha.setYear(year);
            fecha.setHour(0);
            fecha.setMinute(0);
            fecha.setSecond(0);
            // TODO process result here
            java.util.List<aerolinea.Vuelo> result = port.consultarVuelosByDate(fecha);
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }

    }

    static public ArrayList<DTOAerolinea> toDTO(List<Vuelo> lvuelos) {

        ArrayList<DTOAerolinea> dtolist = new ArrayList<DTOAerolinea>();

        for (int i = 0; i < lvuelos.size(); i++) {
            DTOAerolinea dto = new DTOAerolinea();
            dto.setCiudadDestino(lvuelos.get(i).getCiudadDestino().getNombre());
            dto.setCiudadOrigen(lvuelos.get(i).getCiudadOrigen().getNombre());
            dto.setCosto(lvuelos.get(i).getCosto());
            dto.setCupos(lvuelos.get(i).getCupos());
            dto.setDescripcion(lvuelos.get(i).getAvion().getDescripcion());
            Date fecha = new Date(lvuelos.get(i).getFechaSalida().getYear() - 1900, lvuelos.get(i).getFechaSalida().getMonth() - 1, lvuelos.get(i).getFechaSalida().getDay());
            dto.setFecha(fecha);
            dto.setHora(lvuelos.get(i).getHora());
            dto.setIdvuelo(lvuelos.get(i).getId());
            dto.setPlaca(lvuelos.get(i).getAvion().getPlaca());

            dtolist.add(dto);
        }

        return dtolist;
    }

    static public ArrayList<DTOAerolinea> getVuelosvista(int year, int month, int day) throws Exception {

        return toDTO(getvuelo(year, month, day));
    }

    static public ArrayList<DTOAerolinea> getVuelosvista(int idorigen, int iddestino, int year, int month, int day) throws Exception {

        return toDTO(getvuelos(idorigen, iddestino, year, month, day));
    }

    static public Vuelo getvuelobyid(int idvuelo) throws Exception {


        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here
            long id = (long) idvuelo;
            // TODO process result here
            aerolinea.Vuelo result = port.retornarVueloById(id);
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }

    }

    static public Reserva reservar(String username, int idvuelo, int year, int month, int day) throws Exception {


        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here
            aerolinea.Reserva reserva = new aerolinea.Reserva();
            // TODO process result here
            reserva.setCliente(getclienteaerolinea(username));
            reserva.setVuelo(getvuelobyid(idvuelo));
            DatatypeFactory factory = DatatypeFactory.newInstance();
            XMLGregorianCalendar fecha = factory.newXMLGregorianCalendar();
            fecha.setDay(day);
            fecha.setMonth(month);
            fecha.setYear(year);
            fecha.setHour(0);
            fecha.setMinute(0);
            fecha.setSecond(0);
            reserva.setFecha(fecha);
            aerolinea.Reserva result = port.crearReserva(reserva);
            System.out.println("Result = " + result);
            return result;
        } catch (Exception ex) {
            throw ex;
        }
}

    static public String getreserva(String id1) throws Exception{
        try { // Call Web Service Operation
            aerolinea.AerolineaServicesService service = new aerolinea.AerolineaServicesService();
            aerolinea.AerolineaServices port = service.getAerolineaServicesPort();
            // TODO initialize WS operation arguments here
            long id = Long.parseLong(id1);
            // TODO process result here
            aerolinea.Reserva result = port.consultarReservaById(id);
            System.out.println("Result = "+result);
            String r = "<br>Ciudad origen:"+result.getVuelo().getCiudadOrigen()+"<br>Ciudad destino"+result.getVuelo().getCiudadDestino()+
                    "<br>Fecha:"+result.getFecha().getYear()+"-"+result.getFecha().getMonth()+"-"+result.getFecha().getDay()+
                    "<br>Placa avion:"+result.getVuelo().getAvion().getPlaca()+"<br>Descripcion:"+result.getVuelo().getAvion().getDescripcion();
            return r;
        } catch (Exception ex) {
            throw ex;
        }
    }
}
